package com.quanyan.user.web;

import com.quanyan.api.APIResponse;
import com.quanyan.common.user.AppUserCacheInfo;
import com.quanyan.platform.client.IGatherTagService;
import com.quanyan.platform.response.GatherTagItem;
import com.quanyan.platform.response.RespGatherTags;
import com.quanyan.redisCluster.config.RedisTemplate;
import com.quanyan.redisCluster.utils.RedisKey;
import com.quanyan.user.constants.UserConstants;
import com.quanyan.user.model.vo.request.ReqEditUserInfoParam;
import com.quanyan.user.model.vo.request.ReqUserBaseParam;
import com.quanyan.user.model.vo.request.ReqUserInfo;
import com.quanyan.user.model.vo.request.ReqUserVerify;
import com.quanyan.user.model.vo.response.*;
import com.quanyan.user.model.vo.response.my.GatherBaseInfo;
import com.quanyan.user.request.ReqChangePhone;
import com.quanyan.user.service.UserInformationService;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 用户信息控制中心
 * Created by zhang on 16/7/1.
 */
@RestController
@RequestMapping("/api/user/information")
public class UserInformationController extends UserBaseController{
    private static final org.slf4j.Logger logger = LoggerFactory.getLogger(UserInformationController.class);

    @Autowired
    private UserInformationService userInformationService;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private IGatherTagService iGatherTagService;

    /**
     * 用户实名认证
     * @param reqUserVerify
     * @param request
     * @return
     */
    @RequestMapping(value = "verify", method = RequestMethod.POST)
    public APIResponse verify(@Valid @RequestBody ReqUserVerify reqUserVerify,HttpServletRequest request){
        int uid = (int)request.getAttribute("uid");
        boolean flag = userInformationService.verify(reqUserVerify,uid);
        if(flag){
            return APIResponse.returnSuccess();
        }else{
            return APIResponse.returnFail("实名认证失败，请重新认证");
        }
    }

    /**
     * 获取用户信息
     * @param request
     * @return
     */
    @RequestMapping(value = "get", method = RequestMethod.POST)
    public APIResponse<RespUserInfo> getUserInfo(HttpServletRequest request){
        int uid = (int) request.getAttribute("uid");
        RespUserInfo respUserInfo = this.userInformationService.getUserInfo(uid);
        if(null != respUserInfo){
            return APIResponse.returnSuccess(respUserInfo);
        }else{
            return APIResponse.returnFail("个人信息获取失败，请重新获取");
        }
    }

    /**
     * 用户信息更新
     * @param reqUserInfo
     * @param request
     * @return
     */
    @RequestMapping(value = "update", method = RequestMethod.POST)
    public APIResponse updateUserInfo(@RequestBody ReqUserInfo reqUserInfo,HttpServletRequest request){
        int uid = (int) request.getAttribute("uid");
        int n = this.userInformationService.updateUserInfo(reqUserInfo,uid);
        if(n>0){
            return APIResponse.returnSuccess();
        }else{
            return APIResponse.returnFail("更新失败，请重新提交");
        }
    }

    /**
     * 我的二维码名片
     * @param request
     * @return
     */
    @RequestMapping(value = "myQRCode", method = RequestMethod.POST)
    public APIResponse<RespUserBaseInfo> myQRCode(HttpServletRequest request){
        int uid = (int) request.getAttribute("uid");
        RespUserBaseInfo respUserBaseInfo = this.userInformationService.myQRCode(uid);
        return APIResponse.returnSuccess(respUserBaseInfo);
    }

    /**
     * 我的首页信息
     * @param request
     * @return
     */
    @RequestMapping(value = "my", method = RequestMethod.POST)
    public  APIResponse<RespUserMine> my(HttpServletRequest request){
        int uid = 0;
        String dsn ="";
        int userType;
        if (null != request.getAttribute("uid")) {
            uid = (int) request.getAttribute("uid");
            userType = UserConstants.USER_REGISTER;
        }else{
            dsn = (String) request.getAttribute("dsn");
            userType = UserConstants.USER_VISITOR;
        }
        RespUserMine respUserMine;
        respUserMine= this.userInformationService.my(uid,dsn,userType);
        if(null == respUserMine){
            return APIResponse.returnFail("首页信息获取失败，请重新获取");
        }else{
            return APIResponse.returnSuccess(respUserMine);
        }
    }

    /**
     * 我的主页
     * @param map
     * @param request
     * @return
     */
    @RequestMapping(value = "index", method = RequestMethod.POST)
    public  APIResponse<RespUserIndex> index(@RequestBody Map map,HttpServletRequest request ){
        Integer userType;
        Integer uid = 0;
        if(null == request.getAttribute("uid") || request.getAttribute("uid").equals(0)){
            userType = UserConstants.USER_VISITOR;
        }else{
            userType = UserConstants.USER_REGISTER;
            uid = (Integer) request.getAttribute("uid");
        }

        if(null == map.get("itemType")){
            return APIResponse.returnFail("请传入必要参数");
        }
        if(null == map.get("currentUid")){
            return APIResponse.returnFail("请传入必要参数");
        }
        String itemType = (String) map.get("itemType");
        int currentUid = (int) map.get("currentUid");
        RespUserIndex respUserIndex;
        respUserIndex = this.userInformationService.index(uid,itemType,currentUid,userType);

        if(null != respUserIndex){
            return APIResponse.returnSuccess(respUserIndex);
        }else{
            return APIResponse.returnFail("个人主页信息获取失败，请重新获取");
        }
    }

    /**
     * 更新用户基础信息
     * @param requestBaseParam
     * @param request
     * @return
     */
    @RequestMapping("updateUserBaseInfo")
    public APIResponse updateUserBaseInfo(@RequestBody ReqUserBaseParam requestBaseParam, HttpServletRequest request){

        int uid = (int) request.getAttribute("uid");
        int n = this.userInformationService.updateUserBaseInfo(requestBaseParam,uid);
        if (n>0) {
            return APIResponse.returnSuccess();
        }else{
            return APIResponse.returnFail("用户基础信息更新失败");
        }
    }

    /**
     * 更换用户手机号
     * @param reqChangePhone
     * @param request
     * @return
     */
    @RequestMapping(value = "changePhone")
    public APIResponse changePhone(@RequestBody ReqChangePhone reqChangePhone,HttpServletRequest request){
        int uid = (int) request.getAttribute("uid");
        reqChangePhone.setUid(uid);
        return userInformationService.changePhone(reqChangePhone);
    }

    /**
     * 获取用户状态信息
     * @param request
     * @return
     */
    @RequestMapping(value = "getUserState",method = RequestMethod.POST)
    public APIResponse<RespUserState> getUserState(HttpServletRequest request){
        int uid = (int) request.getAttribute("uid");

        return userInformationService.getUserState(uid);
    }

    /**
     * 随机产生一批标签
     *
     * @param map
     * @param request
     * @return
     */
    @RequestMapping(value = "/randomGenerateLables", method = RequestMethod.POST, produces = "application/json")
    public APIResponse randomGenerateLables(@RequestBody Map map, HttpServletRequest request){
        Integer pageSize = (Integer) map.get("pageSize");
        return userInformationService.randomGenerateLables(pageSize);
    }

    /**
     * 获取聚集地信息
     *
     * @return
     */
    @RequestMapping(value = "/getGatherBaseInfos", method = RequestMethod.POST, produces = "application/json")
    public APIResponse getGatherBaseInfos(HttpServletRequest request){
        int uid = (int) request.getAttribute("uid");
        try {
            APIResponse apiResponse = iGatherTagService.getGatherTags(uid,uid,2);
            List<GatherTagItem> gatherTagItems = null;//获取我的聚集地信息
            if (apiResponse != null && apiResponse.getData() != null ){
                RespGatherTags respGatherTags = (RespGatherTags)apiResponse.getData();
                if (respGatherTags != null) {
                    gatherTagItems = respGatherTags.getTagItems();
                }
            }
            return  APIResponse.returnSuccess(gatherTagItems);
        }catch (Exception e) {
            logger.error("获取我的聚集地信息失败！：{}",e);
            return APIResponse.returnFail("获取我的聚集地信息失败");
        }

    }


    /**
     * 用户信息更新
     * @param userInfoParam
     * @param request
     * @return
     */
    @RequestMapping(value = "editUserInfo", method = RequestMethod.POST)
    public APIResponse editUserInfo(@RequestBody ReqEditUserInfoParam userInfoParam, HttpServletRequest request){
        if (userInfoParam == null ) {
            return APIResponse.returnFail("请填入需要更改的用户信息");
        }
        int uid = (int) request.getAttribute("uid");
        boolean result = userInformationService.editUserInfo(uid, userInfoParam);
        if(result){
            return APIResponse.returnSuccess();
        }else{
            return APIResponse.returnFail("更新失败，请重新提交");
        }
    }

    /**
     *  获取我的主页 他的主页信息
     * @param map
     * @param request
     * @return
     */
    @RequestMapping(value = "getUserIndex", method = RequestMethod.POST)
    public APIResponse getUserIndex(@RequestBody Map map, HttpServletRequest request){
        String userCookiesName = request.getHeader("userCookiesName");
        RedisKey redisKey = new RedisKey(userCookiesName, "");
        AppUserCacheInfo appUserCacheInfo;
        try {
            Integer uid = null;
            appUserCacheInfo = (AppUserCacheInfo) redisTemplate.getObjectFromC(redisKey, AppUserCacheInfo.class);
            if (appUserCacheInfo != null){
                uid = appUserCacheInfo.getUser_id();
            }
            Integer taUid = (Integer) map.get("taUid");
            return userInformationService.getUserIndex(uid, taUid);
        }catch (Exception e) {
            logger.error("获取用户主页失败!:{}", e);
            return  APIResponse.returnFail("获取用户主页失败！");
        }
    }


    @RequestMapping(value = "updateUserRedis", method = RequestMethod.POST)
    public APIResponse updateUserRedis(@RequestBody Map map, HttpServletRequest request){
        List<Integer> uids = (List<Integer>) map.get("uids");
        return userInformationService.updateUserRedis(uids);
    }

}
